/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.beynet.docs.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import org.beynet.docs.http.HttpUploaderListener;
import org.beynet.docs.sync.FileToSync;

/**
 *
 * @author beynet
 */
public class FileUploadingTableModel extends DefaultTableModel implements HttpUploaderListener {

    /**
     * 
     */
    private static final long serialVersionUID = -2188448273263597299L;

    public FileUploadingTableModel() {
        toBeSynced = new ArrayList<FileToSync>();
    }
    private static final ColumnContext[] columnArray = {
        new ColumnContext("No.", Integer.class, false),
        new ColumnContext("Name", String.class, false),
        new ColumnContext("Progress", Integer.class, false)
    };

    @Override
    public boolean isCellEditable(int row, int col) {
        return columnArray[col].isEditable;
    }

    @Override
    public Class<?> getColumnClass(int modelIndex) {
        return columnArray[modelIndex].columnClass;
    }

    @Override
    public int getColumnCount() {
        return columnArray.length;
    }

    @Override
    public String getColumnName(int modelIndex) {
        return columnArray[modelIndex].columnName;
    }

    @Override
    public void addFileToObserver(FileToSync fileToSync) {
        toBeSynced.add(fileToSync);
        Object[] obj = {Integer.valueOf(toBeSynced.size()), fileToSync.getFile().getName(), Integer.valueOf(0)};
        super.addRow(obj);
    }

    private static class ColumnContext {

        public final String columnName;
        public final Class columnClass;
        public final boolean isEditable;

        public ColumnContext(String columnName, Class columnClass, boolean isEditable) {
            this.columnName = columnName;
            this.columnClass = columnClass;
            this.isEditable = isEditable;
        }
    }

    /**
     * called when a file uploading state has changed
     * @param o
     * @param o1 
     */
    @Override
    public void update(Observable o, Object o1) {
        if (o instanceof FileToSync) {
            FileToSync current = (FileToSync) o;
            synchronized(toBeSynced) {
                int index = toBeSynced.indexOf(current);
                if (index==-1) {
                    logger.error("received event for unknown file");
                    return;
                }
                Integer value;
                if (o1 == null) {
                    if (FileToSync.Status.PROCESSED.equals(current.getStatus())) {
                        System.out.println("File " + current.getFile().getName() + " completed");
                        value = Integer.valueOf(100);
                        setValueAt(value, index, 2);
                    }
                    else if (FileToSync.Status.NO_CHANGE.equals(current.getStatus())) {
                        System.out.println("File " + current.getFile().getName() + " did not change");
                        value = Integer.valueOf(-1);
                        setValueAt(value, index, 2);
                    }
                } else {
                    Map<String, Long> rate = (Map<String, Long>) o1;
                    Long sent = rate.get(FileToSync.FILE_SENT);
                    Long size = rate.get(FileToSync.FILE_SIZE);
                    int res = (int) ((1.0 * sent.longValue()) / size.longValue() * 100);
                    setValueAt(res, index, 2);
                }
            }
        }
    }

    /**
     * remove all the files selected
     */
    public void clearFiles() {
        synchronized (toBeSynced) {
            for (int i = toBeSynced.size()-1; i >=0; i--) {
                removeRow(i);
            }
            toBeSynced.clear();
        }
    }
    public final List<FileToSync> toBeSynced;
    private final static Logger logger = Logger.getLogger(FileUploadingTableModel.class);
}
